<template>
  <div class="wangeditor">
    <Toolbar class="wangeditor-toolbar" :editor="editor" :default-config="toolbarConfig" :mode="mode" />
    <Editor
      v-model="html"
      class="wangeditor-editor"
      style="overflow-y: hidden"
      :style="{ height: height }"
      :default-config="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
      @onChange="onChange"
    />
  </div>
</template>

<script>
import { Boot } from "@wangeditor/editor";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import formulaModule from "@wangeditor/plugin-formula";
// 注册。要在创建编辑器之前注册，且只能注册一次，不可重复注册。
Boot.registerModule(formulaModule);

export default {
  name: "AtEditor",
  components: { Editor, Toolbar },
  props: {
    height: {
      type: [String, Number],
      default: "300px",
    },
    placeholder: {
      type: String,
      default: "请输入内容...",
    },
  },
  data() {
    return {
      editor: null,
      html: "",
      toolbarConfig: {
        toolbarKeys: [
          // "blockquote",
          "headerSelect",
          "fontSize",
          "fontFamily",
          "lineHeight",
          "|",
          "bold",
          "underline",
          "italic",
          "through",
          "color",
          "bgColor",
          "clearStyle",
          "|",
          "bulletedList",
          "numberedList",
          "justifyLeft",
          "justifyRight",
          "justifyCenter",
          "|",
          // "insertLink",
          {
            key: "group-image",
            title: "图片",
            iconSvg:
              '<svg viewBox="0 0 1024 1024"><path d="M959.877 128l0.123 0.123v767.775l-0.123 0.122H64.102l-0.122-0.122V128.123l0.122-0.123h895.775zM960 64H64C28.795 64 0 92.795 0 128v768c0 35.205 28.795 64 64 64h896c35.205 0 64-28.795 64-64V128c0-35.205-28.795-64-64-64zM832 288.01c0 53.023-42.988 96.01-96.01 96.01s-96.01-42.987-96.01-96.01S682.967 192 735.99 192 832 234.988 832 288.01zM896 832H128V704l224.01-384 256 320h64l224.01-192z"></path></svg>',
            menuKeys: ["insertImage", "uploadImage"],
          },
          "insertTable",
          "insertFormula",
          "|",
          "undo",
          "redo",
          "|",
          "fullScreen",
        ],
      },
      editorConfig: {
        placeholder: this.placeholder,
        hoverbarKeys: {
          formula: {
            menuKeys: ["editFormula"], // “编辑公式”菜单
          },
        },
      },
      mode: "simple", // 'default' or 'simple'
    };
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时，及时销毁编辑器
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错

      setTimeout(() => {
        console.log(this.editor.getAllMenuKeys()); // 当前菜单排序和分组
      }, 0);
    },
    setHtml() {
      const editor = this.editor;
      if (editor) {
        return editor.setHtml();
      }
    },
    getText() {
      const editor = this.editor;
      if (editor) {
        return editor.getText();
      } else {
        return "";
      }
    },
    getHtml() {
      const editor = this.editor;
      if (editor) {
        return editor.getHtml();
      } else {
        return "";
      }
    },
    onChange(editor) {
      this.$emit("onChange", editor);

      console.log(this.getHtml());
    },
  },
};
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>
<style lang="scss" scoped>
.wangeditor {
  position: relative;
  border: 1px solid #ccc;
}
.wangeditor-toolbar {
  border-bottom: 1px solid #ccc;
}
.wangeditor-editor {
  border-bottom: 1px solid #ccc;
}
</style>
